package com.ceba.be.api;

import java.util.HashMap;
import java.util.Map;

import com.ceba.be.api.utils.APIHttpClient;
import com.ceba.be.api.utils.DownLoadSignTest;
import org.json.JSONArray;
import org.json.JSONObject;

public class UploadFileAndMultiSignFromFormByIDTest {
	//测吧签章系统的URL
	private static String cebaSystemUrl = "localhost:8080";
	//本地待签名文件路径
	private static String localPdfPathToBeSigned = "C:\\Users\\liguangyao\\Desktop\\个人通用证书申请表.pdf";
	//本地签名文件路径输出路径
	private static String localSignedPdfPath = "C:\\Users\\liguangyao\\Desktop\\个人通用证书申请表_rsa.pdf";
	//测吧系统签名接口id。在测吧系统管理员配置的“接口可视化配置”设置
	private static String cebaSystemSignId = "524000c746894a1b9010637de650e88b";

	public static void main(String[]args){
		String json = generJson();
		String url= "http://"+cebaSystemUrl+"/ep/cxf/ws/rs/uploadFileAndMultiSignFromFormByID";
        APIHttpClient client = new APIHttpClient(url);
        String re = "";
        Map<String,String> maps = new HashMap<String,String>();
        maps.put("json", json);
		try {
			re = client.uploadFileImpl(url,localPdfPathToBeSigned,"pdf",maps,"text");
		} catch (Exception e) {
			e.printStackTrace();
		}
        System.out.println(re);
        JSONObject o = new JSONObject(re);
        if(o.has("successPath")){
	        JSONArray arr = o.getJSONArray("successPath");
	        DownLoadSignTest.downloadFile("http://"+cebaSystemUrl+arr.getString(0), localSignedPdfPath);
        }
	}

	private static String generJson() {
		StringBuffer ss = new StringBuffer();
		ss.append("{"
			+ "\"sign\":")
			.append("{")
			.append("\"IDs\": \"" + cebaSystemSignId + "\"")
//			.append(",\"isSignature\":\"false\"")
			.append("}}");
	
		return ss.toString();
	}
}
